<?
#include('../rp2/global_functions.php');

$dbpass = "w9JwRoA";
$dbuser = "root";
$dbname = "rp2";
$dbhost = "209.25.134.4";

if(!$dbh_rp2 = pg_connect("dbname=$dbname host=$dbhost user=$dbuser password=$dbpass")) {
	$errorMsg = translate('int_error');
	include_once('errorMsg.html');
	exit;
}
//exit('Error Processing your request');

//-----------------------------------------------------
$sql['type'] = "SELECT";
$sql['params'] = "c.client_id,c.reseller_id,c.store_id,c.username,s.name,s.title,s.currency,s.products";
$sql['table'] = "clients c left join stores s using (store_id)";
$sql['where'] = "c.username='$client'";
$sql['options'] = "";
$sql['return'] = "hash";
//------------------------------------------------------
$client=sql_exec($sql,$dbh_rp2,'assoc');

if(!is_array($client)) {
  $errorMsg = translate('int_error');
	include_once('errorMsg.html');
	exit;
}
// exit('Error. Please Contact the support');
//------------------------------------------------------
$sql['type'] = "SELECT";
$sql['params'] = "purchase_id,period,
		expire_date::date as expire_date,
		(expire_date+ ((case when p.life=-1 then p1.period else p.life end)|| ' months')::interval)::date as new_expire_date,
		p.product_id,p.type,p.name,p.price,p.wholesale,p.services";
$sql['table'] = "purchases p1 left join products p using(product_id)" ;
$sql['where'] = "p1.client_id='$client[client_id]' and p1.start_date<now()";
$sql['options'] = "";
$sql['return'] = "struct";
//------------------------------------------------------
$products= sql_exec($sql,$dbh_rp2,'assoc');

if(!is_array($products)) {
	$errorMsg = translate('int_error');
	include_once('errorMsg.html');
	exit;
}
// exit('We are sorry but we cannot process at the moment [#121]');
foreach($products as $p){
	if($p[type]=='plan') $plan_expire_date=$p[expire_date];
}

if(is_array($products))
	foreach($products as $product){
		if($product[type] == 'plan'){
			$content.="<tr><td>&nbsp;<input type='hidden' name='products[$product[purchase_id]]' value='1'></td><td>$product[name] $product[type]</td><td colspan=2>$product[expire_date]</td><td>$product[new_expire_date]</td>";
			$content.="</tr>";
			$content.="<tr><td colspan=5 align=center>Renewal Cycle: <select name=period>";

			$client[products][periods][$product[product_id]][$product[period]]='on'; //tekushtiq plan se razreeshava za renew sas perioda za koito e zakupen nezavisimo ot products offering na resellera
			foreach(array_keys($client[products][periods][$product[product_id]]) as $pers) $content.="<option value=$pers>$pers Month(s)</option>";
			$content.="</select></td></tr>";
		}else if($product[type] == 'service' && $product['expire_date']==$plan_expire_date){
			$q=get_service_quantity($product[name]);
			$content2.="<tr><td><input type='checkbox' name='products[$product[purchase_id]]' value='1' checked></td><td>".get_service_name($product[name])." $product[type]</td><td>$q[1]</td><td>$product[expire_date]</td><td>$product[new_expire_date]</td></tr>";
		}elseif($product[type]=='domain'){
			$content3.="<tr><td><input type='checkbox' name='products[$product[purchase_id]]' value='1' checked></td><td colspan=2>".get_service_name($product[name])." $product[type]</td><td>$product[expire_date]</td><td>$product[new_expire_date]</td></tr>";

		}


	}
if($content3) $content.="<tr><th colspan=3>Renew Domains </th><th>Expiring</th><th>New Expiration</th></tr>".$content3;
if($content2) $content.="<tr><th colspan=2>Renew Installed Services </th><th>Quantity</th><th>Expiring</th><th>New Expiration</th></tr>".$content2;

?>
<br><br>
<table class=bTable cellspacing=0>
<?
$params[sid]=$sid;
$params[action]='renewClientTotal';
$params[client]=$client[username];
$url="$baseURL/$webDIR/?".encode_params($params);
?>
<form action='<?=$url?>' method='POST'>
<tr><th colspan=2>Renew your plan:</th><th colspan=2>Expiring</th><th>New Expiration</th></tr>
<?=$content?>
<tr><td colspan=10 align=center><input type='submit' value='Continue'></td></tr>
</table>



